package com.action.service.pressenter;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.action.childbase.ChildBasePressent;
import com.action.entity.HistoryStock;
import com.action.entity.UserAliSncode;
import com.action.entity.UserHelper;
import com.action.entity.XyBUserRecvRed;
import com.action.entity.rqview.SmsRequestView;
import com.action.service.ChildRestServer;
import com.base.app_core.base.ActionPacketView;
import com.base.app_core.base.BaseView;
import com.base.app_core.model.Paginated;
import com.base.app_core.model.UserSmsVo;
import com.base.app_core.model.params.BindMobileParam;
import com.base.app_core.model.params.GetSmsCodeParam;
import com.base.app_core.service.Retrofit.RestServer;
import com.base.app_core.service.rx.Callback;
import com.base.app_core.service.user.model.BindAliUserAcount;
import com.base.app_core.util.AppConstants;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * Created by jiansheng.li on 2018/3/19.
 */

public class MemberPressenter extends ChildBasePressent<BaseView> {


    @Inject
    public MemberPressenter(ChildRestServer mRestfull, RestServer server) {
        super(mRestfull, server);
    }

    private int mTick = 0;
    protected Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (null != msg)
                switch (msg.what) {
                    case 0:
                        mTick--;
                        if (mTick >= 0 && isnotNull()) {
                            ((ActionPacketView.SmsAction) mLifecycle.get()).updateSmsCode(mTick);
                            handler.sendEmptyMessageDelayed(0, 1000);
                        }
                        break;
                    case 1://token刷新导致获取验证码失败，3s重试获取。
//						getSmsCode(user.getPhoneNo());
                        break;
                    default:
                        break;
                }
        }
    };

    public void getSmsCode(String phoneNo) {
        invoke(mRestfull.getSmsCode(new GetSmsCodeParam(phoneNo, "0")), new Callback<UserSmsVo>() {
            @Override
            public void onResponse(UserSmsVo data, Paginated paginated,Object extraData) {
                super.onResponse(data, paginated,extraData);
            }
        });
    }

    public void updateHint() {
        handler.removeMessages(0);
        mTick = 60;
        handler.sendEmptyMessage(0);
    }

    //绑定手机号码
    public void bindUserMobile(String phone, String smscode) {
        SmsRequestView src = new SmsRequestView();
        src.setMobile(phone);
        src.setSmsCode(smscode);
        invoke(mChildRest.bindUserMobile(new BindMobileParam(phone, smscode, "0")), new Callback<BindAliUserAcount>() {
            @Override
            public void onResponse(BindAliUserAcount data, Paginated paginated,Object extraData) {
                if (isnotNull() && mLifecycle.get() instanceof ActionPacketView.SmsAction) {
                    ((ActionPacketView.SmsAction) mLifecycle.get()).bindMobileSuccess(data);
                }
            }
        });
    }

    //获取收到的红包列表
    public void fetchRecvHistoryRedPackets(int pageNo) {
//        @Query(AppConstants.FIELD.PAGE) int pageNo,
//        @Query(AppConstants.FIELD.PAGESIZE) int pagesize
        Map<String, Object> map = new HashMap<>();
        map.put(AppConstants.FIELD.PAGE, pageNo);
        map.put(AppConstants.FIELD.PAGESIZE, AppConstants.Const.PAGESIZE);
        invoke(mChildRest.fetchRecvHistoryRedPackets(map), new Callback<List<XyBUserRecvRed>>());
    }

    //获取发送的红包列表
    public void fetchSendHistoryRedPackets(int pageNo) {
        Map<String, Object> map = new HashMap<>();
        map.put(AppConstants.FIELD.PAGE, pageNo);
        map.put(AppConstants.FIELD.PAGESIZE, AppConstants.Const.PAGESIZE);
        invoke(mChildRest.fetchSendHistoryRedPackets(map), new Callback<List<XyBUserRecvRed>>() {
            @Override
            public void onResponse(List<XyBUserRecvRed> data, Paginated paginated, Object extraData) {
                if (isnotNull()) {
                    mLifecycle.get().notifyData(data, XyBUserRecvRed.class, paginated);
                }
            }
        });
    }

    /**
     * 获取历史股票价格
     */
    public void fetchHistoryStockPrice() {
        invoke(mChildRest.fetchHistoryStock(), new Callback<List<HistoryStock>>());
    }

    public void fetchUserHelper() {
        invoke(mChildRest.fetchUserHelper(), new Callback<UserHelper>());
    }

    public void getDefaultAliSncode(String aliSnCode) {
        Map<String,Object> params=new HashMap<>();
        params.put("snCode",aliSnCode);
        invoke(mChildRest.getDefaultAliSncode(params), new Callback<UserAliSncode>());
    }

    
}
